/**
 * Copyright 2011 AJG van Schie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.googlecode.julius.data.define;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import julius.utilities.CollectionHelper;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.googlecode.julius.data.DefineReader;
import com.googlecode.julius.data.SprintMeta;

public class Define4Reader implements DefineReader {
    public static Logger log = Logger.getLogger(Define4Reader.class);

    private final String username;
    private final String password;
    private final String url = "https://tps.define.logica.com/";

    public Define4Reader(final String user, final String pass) {
        this.username = user;
        this.password = pass;
    }

    @Override
    public DefineIssue readItem(final int ticketNr) {
        TracXMLRpcClient reader = new TracXMLRpcClient(url, username, password);
        List<List<String>> tempTicketInfo = reader.getTicketSP(ticketNr);
        DefineIssue issue = new DefineIssue(tempTicketInfo.get(0));

        List<List<String>> ticketChangeLog = reader.getTicketChangelog(ticketNr);
        for (List<String> log : ticketChangeLog) {
            issue.addLog(new DefineLog(log));
        }
        return issue;
    }

    @Override
    public Collection<DefineMilestone> readMilestones() {
        TracXMLRpcClient reader = new TracXMLRpcClient(url, username, password);
        List<DefineMilestone> stones = CollectionHelper.list();
        for (String milestone : reader.getMilestoneList()) {
            stones.add(new DefineMilestone(milestone));
        }
        return stones;
    }

    @Override
    public DefineMilestoneContents readMilestone(final DefineMilestone milestone) {
        TracXMLRpcClient reader = new TracXMLRpcClient(url, username, password);
        DefineMilestoneContents contents = new DefineMilestoneContents(milestone.getName());
        for (String ticket : reader.getTicketList(milestone.getName())) {
            contents.add(new DefineTicketRef(Integer.valueOf(ticket)));
        }
        return contents;
    }

    @Override
    public SprintMeta readMeta(final DefineMilestone stone) {
        TracXMLRpcClient reader = new TracXMLRpcClient(url, username, password);
        HashMap<String, Object> res = reader.getMilestoneInfo(stone.getName());

        Gson gson = new Gson();
        return gson.fromJson((String) res.get("description"), SprintMeta.class);
    }

}
